<template>
	<view class='navbar'>
		<view class="navbar-fixed">
			<!-- 状态栏 （占位） -->
			<!-- #ifndef MP-ALIPAY -->
			<view :style="{height:statusBarHeight+'px'}"></view>
			<!-- #endif -->
			<!-- 导航栏内容 -->
			<view class="navbar-content" :class="{search:isSearch}" :style="{height:navBarHeight+'px', width:windowWidth+'px'}" @click.stop="open">
				<view class="navbar-content_search-icon" v-if="isSearch" @click="back"> 
					<uni-icons type="back" size="22" color="#fff"></uni-icons>
				</view>
				
				<view  v-if="!isSearch" class="navbar-search" :style="{height:navbarSearchHeight+'px'}">
					<!-- 非搜索页显示 -->
				    <view class='navbar-search_icon'>
						<uni-icons type="search" size='16' color="#999"></uni-icons>
					</view>
				    <view class='navbar-search_text'>输入</view>
				</view> 
				<view v-else class="navbar-search">
					<!-- 搜索页显示 -->
					<input class='navbar-search_text' type="text" v-model="value" placeholder="输入搜索内容"
					 @input="inputChange"/>
				</view>
				
			</view>
			
		</view>
	    <view :style="{height:navBarHeight+statusBarHeight+'px'}"></view>
	</view>
</template>

<script>
	export default {
		props:{
			// value:{
			// 	type:[String,Number],
			// 	default:''
			// },
		   isSearch:{
			   type:Boolean,
			   default:false
		   }
		},
		data() {
			return {
				statusBarHeight:20,
				navBarHeight:45,
				navbarSearchHeight:32,
				windowWidth:375,
				value:'',
			};
		},
		created(){
			//获取手机系统信息
			const info = uni.getSystemInfoSync()
			console.log(info)
			//设置状态栏高度
			this.statusBarHeight=info.statusBarHeight;
			this.windowWidth = info.windowWidth
			
			//条件编译
			//#ifndef H5 || APP-PLUS || MP-ALIPAY
			//获取胶囊的位置
			const menuButtonInfo=uni.getMenuButtonBoundingClientRect()
			console.log(menuButtonInfo)
			//导航栏的高度 = （胶囊底部高度 - 状态栏的高度）+（胶囊顶部高度-状态栏的高度）
			this.navBarHeight = (menuButtonInfo.bottom - info.statusBarHeight) + (menuButtonInfo.top - info.statusBarHeight)
			
			//设置搜索框高度
			this.navbarSearchHeight=menuButtonInfo.height
			console.log(this.navbarSearch)
			//设置搜索框宽度
			this.windowWidth = menuButtonInfo.left
			//#endif
			//#ifdef MP-ALIPAY
			this.statusBarHeight = 0
			//#endif
		},
		methods:{
			back(){
				// uni.navigateBack({})
				uni.switchTab({
					url:"/pages/index/index"
				})
			},
			open(){
				if(this.isSearch) return
				uni.navigateTo({
					url:"/pages/home-search/home-search"
				})
			},
			inputChange(e){//回车问题
				const {value} = e.detail
				console.log(value)
				this.$emit("input",value)
			}
		},
		watch:{
			value(newVal){
				this.Val=newVal
			}
		}
		
	}
</script>

<style lang='scss' scoped>
.navbar{
	.navbar-fixed{
		position:fixed;
		top:0;
		left:0;
		z-index:999;
		width:100%;
		background-color: $mk-base-color;
		
		.navbar-content{
			display: flex;
			justify-content: center;
			align-items: center;
			padding:0 15px;
			box-sizing:border-box;
			height:45px;
			
			.navbar-search{
				 display:flex;
				 /* flex-direction: row; */
				 align-items: center;
				 padding:0 10px;
				 width:100%;
				 heigth:30px;
				 background-color: #fff;
				 border-radius: 30px;
				 
				 .navbar-search_icon{
					/* width:10px;
					 height:10px;
					 border:1px red solid; */
					 margin-right:10px; 
				
				 }
				 .navbar-search_text{
					 width:100%;
					 font-size: 14px;
					 color:#999;
				 }
			}
			&.search{
				padding-left:0;
				
				.navbar-content_search-icon{
					margin-left: 10px;
					margin-right: 10px;
				}
				.navbar-search{
					border-radius: 5px;
				}
				.navbar-search_text{
					
					padding:6px 0;
				}
			}
		}

	}
	
}
</style>
